package com.summer.rx.impl;

import java.util.concurrent.TimeUnit;

/**
 * 线程工具
 */
public class ThreadUtil {

    public static <T> Worker<T> interval(long period, TimeUnit unit) {
        return new Worker(period, unit);
    }

    public static <T> Worker<T> once() {
        return new Worker(-1, TimeUnit.MILLISECONDS);
    }

    static int i =0;
    
    public static void main(String[] args) throws InterruptedException {
    	
//    	ThreadUtil.<Integer>once().just(1,2,3,4,5,6,7).map(dir -> {
//            System.out.println("章节目录：" + dir + " 已经删除");
//            return dir;
//        }).accept();
    	
//    	ThreadUtil.interval(1110, TimeUnit.MILLISECONDS).just("sss", "vvv").subscribeOn().onRun(data -> {
    	ThreadUtil.once().just("sss").subscribeBy().onRun(data -> {
    		data += "=====";
    		System.out.println(data + "：action 处理 " + Thread.currentThread().getName());
    	}).subscribeBy().onRun(data -> {
    		System.out.println(data + "：action 处理1 " + Thread.currentThread().getName());
    	}).subscribeOn().onRun(data -> {
    		System.out.println(data + "：action 处理2 " + Thread.currentThread().getName());
//            throw new RuntimeException("ssss");
    	}).subscribeBy().onRun(data -> {
    		System.out.println(data + "：action 处理3 " + Thread.currentThread().getName());
    	}).subscribeBy().onRun(data -> {
    		System.out.println(data + "：action 处理4 " + Thread.currentThread().getName());
    	}).accept();
    	
    	TimeUnit.SECONDS.sleep(2);
    	
    	ThreadUtil.once().subscribeBy().onRun(data -> {
    		System.out.println("action 处理~~~~~~~~~");
    	}).accept();
    	
    	
//    	System.out.println(Thread.currentThread().getName() + " mian ");
//    	
//    	interval(1, TimeUnit.MILLISECONDS)
////    	once()
////    	.map(1)
//    	.subscribeOn()
//    	.map(data -> {
//    		System.out.println(Thread.currentThread().getName() + " 1 " + (i++));
//    		return data;
//    	})
//    	.subscribeBy()
//    	.map(data -> {
//    		System.out.println(Thread.currentThread().getName() + " 2 " + (i++));
//    		return data;
//    	})
//    	.subscribeOn()
//    	.map(data -> {
//    		System.out.println(Thread.currentThread().getName() + " 3 " + (i++));
//    		return data;
//    	})
//    	.subscribeOn()
//    	.map(data -> {
//    		System.out.println(Thread.currentThread().getName() + " 4 " + (i++));
//    		return data;
//    	})
//    	.subscribeOn()
//    	.map(data -> {
//    		System.out.println(Thread.currentThread().getName() + " 5 " + (i++));
//    		return data;
//    	})
//    	.subscribeBy()
//    	.map(data -> {
//    		System.out.println(Thread.currentThread().getName() + " 6 " + (i++));
//    		System.out.println("--------------------------------------------------------");
//    		return data;
//    	})
//    	.subscribeOn()
//    	.accept();
//
//    	System.out.println(Thread.currentThread().getName() + " mian ");
//    	TimeUnit.SECONDS.sleep(100);
	}
    
}
